﻿using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace Handy_WPF.MVVM
{
    /// <summary>
    /// 检查字段是否为空
    /// </summary>
    public class IsNullCheck : ValidationAttribute
    {
        public override bool IsValid(object value)
        {
            var name = value as string;
            return !string.IsNullOrEmpty(name);
        }

        public override string FormatErrorMessage(string name)
        {
            return "不能为空";
        }
    }

    /// <summary>
    /// 检查字段是否为数值
    /// </summary>
    public class LognoExists : ValidationAttribute
    {
        public override bool IsValid(object value)
        {
            if (value == null) return false;

            var name = value as string;
            Regex regex = new Regex("^[0-9]*$");
            return regex.IsMatch(name);
        }

        public override string FormatErrorMessage(string name)
        {
            return "必须为纯数字字符！";
        }
    }
}
